<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  css_bundle :ways_to_contact
%>

<%# this needs to be reactified. badly. %>
<h2>{{#t}}Ways to Contact{{/t}}</h2>
<div>
  <table class="<%= 'single' if @email_channels.length <= 1 %> channel_list email_channels ic-Table ic-Table--condensed">
    <thead>
      <tr>
        <th>{{#t}}Email Addresses{{/t}}</th>
        <th><span class="screenreader-only">{{#t}}Set Email as Default{{/t}}</span></th>
        <th><span class="screenreader-only">{{#t}}Email Actions{{/t}}</span></th>
      </tr>
    </thead>
  <% @email_channels.each do |c| %>
    <tr class="channel <%= "default" if c == @default_email_channel %> <%= "unconfirmed" if c.state == :unconfirmed %> pseudonym_<%= c.pseudonym &&c.pseudonym.id %>" id="channel_<%= c.id %>">
      <th scope="row">
        <% if c.state == :unconfirmed %>
          <a href="#" class="path email_channel contact_channel_path ellipsis" title="<%=h c.path %>"><%= c.path %></a>
        <% else %>
          <p class="path email_channel contact_channel_path ellipsis"><%= c.path %></>
        <% end %>
      </th>
      <td class="email_meta">
        <a href="<%= profile_url %>" class="default_link no-hover" aria-label="{{#t}}Set default email address{{/t}}" >
          {{> icons/star}}
          <% if c == @default_email_channel %>
            <span class="screenreader-only default_label">{{#t}}This is the default email address{{/t}}</span>
          <% else %>
            <span class="screenreader-only">{{#t}}Set as default email address{{/t}}</span>
          <% end %>
          <% if c.state == :unconfirmed %>
            <span class="screenreader-only">{{#t}}This email address is unconfirmed{{/t}}</span>
            {{> icons/warning}}
          <% end %>
        </a>
        <span class="screenreader-only unconfirmed_text">{{#t}}This email address is unconfirmed{{/t}}</span>
      </td>
      <td class="email_actions">
        <span class="hidden_for_single">
          <% if c.state == :unconfirmed && (c.grants_right?(@current_user, :force_confirm) || (@real_current_user && c.grants_right?(@real_current_user, :force_confirm))) %>
              <a href="<%= registration_confirmation_url(c.confirmation_code) %>" class="confirm_channel_link no-hover" aria-label="{{#t}}Confirm Email Address") %>">{{> icons/check}}</a>
          <% end %>
          <% unless !@domain_root_account.edit_institution_email? && c.imported? %>
            <a href="<%= communication_channel_url(c.id) %>" class="delete_channel_link no-hover" aria-label="{{#t}}Remove email address") %>" >{{> icons/trash}}</a>
          <% end %>
          <span class="screenreader-only cant_delete_default_text">{{#t}}You cannot remove your default email address{{/t}}</span>
          <% if c.active? && c.has_merge_candidates? %>
            <a href="<%= registration_confirmation_url(c.confirmation_code) %>" class="merge_users_link no-hover" aria-label="{{#t}}Another user has the same email address.  Click here to combine user accounts.") %>">{{> icons/group-new}}</a>
          <% end %>
          <span class="pseudonym_id" style="display: none;"><%= c.pseudonym_id %></span>
          <span class="channel_id" style="display: none;"><%= c.id %></span>
          <span class="user_id" style="display: none;"><%= c.user_id %></span>
        </span>
        <% if c.bouncing? %>
          <span class="bouncing-channel" data-tooltip="left" title="{{#t}}Canvas was unable to send email to this address. Please ensure the address is correct to continue receiving email from Canvas.') %>" aria-label="<%= t('bouncing_email', 'Canvas was unable to send email to this address. Please ensure the address is correct to continue receiving email from Canvas.{{/t}}">
            <i class="text text-warning icon-warning"></i>
          </span>
        <% end %>
        <% if c.bounce_count > 0 && (c.grants_right?(@current_user, :reset_bounce_count) || (@real_current_user && c.grants_right?(@real_current_user, :reset_bounce_count))) %>
          <a href="<%= api_v1_reset_bounce_count_url(c.user_id, c.id) %>" class="reset_bounce_count_link no-hover" aria-label="{{#t}}Reset this email address's bounce count") %>"> {{> icons/refresh}}</a>
        <% end %>
      </td>
    </tr>
  <% end %>
  <tr class="channel blank unconfirmed" style="display: none;">
    <th scope="row">
      <a href="#" class="path email_channel contact_channel_path ellipsis" title="">&nbsp;</a>
    </th>
    <td class="email_meta">
      <a href="<%= user_profile_url(@current_user) %>" class="default_link no-hover">{{> icons/star}}</a>
      <span class="screenreader-only unconfirmed_text">{{#t}}This email address is unconfirmed{{/t}}</span>
    </td>
    <td class="email_actions hidden_for_single">
      <a href="<%= communication_channel_url("{{ channel_id }}") %>" class="delete_channel_link no-hover" aria-label="{{#t}}Remove email address') %>">{{> icons/trash}}</a>
      <span class="pseudonym_id" style="display: none;">&nbsp;</span>
      <span class="channel_id" style="display: none;">&nbsp;</span>
      <span class="user_id" style="display: none;">&nbsp;</span>
    </td>
  </tr>
  <tr>
    <td colspan="3" style="text-align: center;">
      <a href="#" class="add_email_link icon-add" title="{{#t}}Email Address{{/t}}</a>
    </td>
  </tr>
</table>
<div class="content-box-mini">
  <table class="channel_list other_channels ic-Table ic-Table--condensed">
    <thead>
      <tr>
        <th>{{#t}}Other Contacts{{/t}}</th>
        <th colspan='2'>{{#t}}Type{{/t}}</th>
      </tr>
    </thead>
    <tbody>
    <% @other_channels.each do |c| %>
      <tr class="channel <%= "unconfirmed" if c.state == :unconfirmed %> pseudonym_<%= c.pseudonym_id %>" id="channel_<%= c.id %>">
        <td><a href="#" class="path contact_channel_path ellipsis"><%= c.path_description %></a></td>
        <td><%= c.path_type %></td>
        <td style="padding: 0; text-align: center;">
          <% if c.bouncing? %>
            <span class="bouncing-channel" data-tooltip="left" title="{{#t}}Canvas was unable to send messages to you using this contact method. Please ensure the contact information is correct to continue receiving messages from Canvas.') %>" aria-label="<%= t('bouncing_channel', 'Canvas was unable to send messages to you using this contact method. Please ensure the contact information is correct to continue receiving messages from Canvas.{{/t}}">
              <i class="text text-warning icon-warning"></i>
            </span>
            <% if c.bounce_count > 0 && (c.grants_right?(@current_user, :reset_bounce_count) || (@real_current_user && c.grants_right?(@real_current_user, :reset_bounce_count))) %>
              <a href="<%= api_v1_reset_bounce_count_url(c.user_id, c.id) %>" class="reset_bounce_count_link no-hover" aria-label="{{#t}}Reset this contact method's bounce count") %>"> {{> icons/refresh}}</a>
            <% end %>
          <% end %>
          <% if c.state == :unconfirmed && (@user.grants_right?(@current_user, :manage_user_details) || (@real_current_user && @user.grants_right?(@real_current_user, :manage_user_details))) %>
              <a href="<%= registration_confirmation_url(c.confirmation_code) %>" class="confirm_channel_link no-hover">{{> icons/check}}<span class="screenreader-only">{{#t}}Confirm contact method{{/t}}</span></a>
          <% end %>
          <% unless c.id == c.user.otp_communication_channel_id %>
            <a href="<%= communication_channel_url(c.id) %>" class="delete_channel_link no-hover">{{> icons/trash}}<span class="screenreader-only"><%= t("Remove contact method{{/t}}</span></a>
          <% end %>
          <span class="pseudonym_id" style="display: none;"><%= c.pseudonym_id %></span>
          <span class="channel_id" style="display: none;"><%= c.id %></span>
          <span class="user_id" style="display: none;"><%= c.user_id %></span>
        </td>
      </tr>
    <% end %>
    <tr class="channel blank unconfirmed" style="display: none;">
      <td><a href="#" class="path">&nbsp;</a></td>
      <td>{{#t}}sms{{/t}}</td>
      <td style="padding: 0; text-align: center;">
        <a href="<%= communication_channel_url("{{ channel_id }}") %>" class="delete_channel_link no-hover">{{> icons/trash}}</a>
        <span class="pseudonym_id" style="display: none;">&nbsp;</span>
        <span class="channel_id" style="display: none;">&nbsp;</span>
        <span class="user_id" style="display: none;">&nbsp;</span>
      </td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
      <td colspan="3" style="text-align: center;">
        <a href="#" class="add_contact_link icon-add" title="{{#t}}Contact Method{{/t}}</a>
      </td>
    </tr>
    </tfoot>
  </table>
</div>
</div>
<div id="communication_channels" style="display: none;">
<ul>
  <li><a href="#register_email_address">{{#t}}Email{{/t}}</a></li>
  <li><a href="#register_sms_number">{{#t}}Text (SMS){{/t}}</a></li>
</ul>
<%= form_tag communication_channels_url, {:id => "register_email_address", :class => 'form-horizontal'} do %>
    <input type="hidden" name="communication_channel[type]" value="email"/>
    <div class="control-group">
      <%= label :communication_channel, :address, t('labels.email_address', 'Email Address'), :class => 'control-label' %>
      <div class="controls">
        <input type="email" name="communication_channel_email" id="communication_channel_email"/>
      </div>
    </div>

    <% if @domain_root_account == Account.default %>
      <div class="control-group">
        <div class="controls">
          <label class='checkbox' for="build_pseudonym_for_email">
            <input type="checkbox" id="build_pseudonym_for_email" name="build_pseudonym" value="1"/>
            {{#t}}I want to log in to Canvas using this email address{{/t}}
          </label>
        </div>
      </div>
    <% end %>

    <div class="form-actions">
      <button type="submit" class="btn btn-primary">{{#t}}Register Email{{/t}}</button>
    </div>
<% end %>
<%= form_tag communication_channels_url, {:id => "register_sms_number", :class => 'form-horizontal'} do %>
    <input type="hidden" name="communication_channel[type]" value="sms"/>

    <% if @user.account.feature_enabled?(:international_sms) %>
      <div class="control-group">
        <label for="communication_channel_sms_country" class='control-label'>{{#t}}Country{{/t}}</label>
        <div class="controls">
          <select class="user_selected country" id="communication_channel_sms_country" name="communication_channel_sms_country">
            <option value="undecided">[{{#t}}Select Country{{/t}}]</option>
            <% CommunicationChannel.country_codes.each do |(numeric_code, name, use_email)| %>
              <option value="<%= numeric_code %>" data-use-email="<%= use_email %>"><%= name %></option>
            <% end %>
          </select>
        </div>
      </div>
    <% end %>

    <div class="control-group">
      <label for='communication_channel_sms_number' class='control-label'>{{#t}}Cell Number{{/t}}</label>
      <div class="controls">
        <input type="tel" name="communication_channel_sms_number" id='communication_channel_sms_number' class="user_selected sms_number" aria-describedby="communication_channel_sms_number_description" /><br/>
        <span style="font-size: 0.8em; color: #a44" id='communication_channel_sms_number_description' class="should_be_10_digits">
          {{#t}}should be 10-digit number{{/t}}
        </span>
        <span class="intl_rates_may_apply">
          {{#t}}International text messaging rates may apply.{{/t}}
        </span>
      </div>
    </div>

    <div class="control-group sms_email_group">
      <label for='communication_channel_carrier' class='control-label'>{{#t}}Carrier{{/t}}</label>
      <div class="controls">
        <select class="user_selected carrier" id='communication_channel_carrier'>
          <option value="other">{{#t}}[Select Carrier]{{/t}}</option>
          <% CommunicationChannel.sms_carriers_by_name.each do |(carrier, domain)| %>
            <option value="#@<%= domain %>"><%= carrier %></option>
          <% end %>
          <option value="other">{{#t}}Other Carrier{{/t}}</option>
        </select>
      </div>
    </div>

    <div class="control-group sms_email_group">
      <label for='communication_channel_sms_email' class='control-label'>{{#t}}SMS Email{{/t}}</label>
      <div class="controls">
        <input type="email" name="communication_channel_sms_email" class="sms_email" id="communication_channel_sms_email" aria-describedby="communication_channel_sms_number communication_channel_carrier" />
      </div>
    </div>

    <div class="form-actions">
      <button type="submit" class="btn btn-primary">{{#t}}Register SMS{{/t}}</button>
    </div>
<% end %>
</div>
<div id="confirm_email_channel" style="display: none;" class="dialog" role="dialog">
  <div style="text-align: center;"><%= t(<<-TEXT, :email_address => raw('<span class="path" style="font-weight: bold;">&nbsp;</span>'))
    We emailed a confirmation link to %{email_address}. Click the link in that
    email to finish registering. Make sure to check your spam box in case it
    got filtered.
    TEXT
  %></div>
  <div style="text-align: center; margin: 10px 0; font-weight: bold;">
    <a href="#" class="re_send_confirmation_link">{{#t}}Re-Send Confirmation{{/t}}</a>
    <a href="<%= re_send_confirmation_url("{{ user_id }}", "{{ id }}") %>" class="re_send_confirmation_url" style="display: none;">&nbsp;</a>
  </div>
  <div class="button-container">
    <button type="button" class="btn cancel_button">{{#t}}Ok, Thanks{{/t}}</button>
  </div>
</div>
<%= form_tag "", {:id => "confirm_communication_channel", :style => "display: none;", :class => "dialog", :role => "dialog"} do %>
  <input type="hidden" name="pseudonym_id" value=""/>
  <input type="hidden" name="channel_id" value=""/>
  <input type="hidden" name="user_id" value=""/>
  <div id='sms_confirmation_instructions' style="text-align: center;">
  <%= t(<<-TEXT, :sms_number => raw('<span class="path" style="font-weight: bold;">&nbsp;</span>'))
    To activate SMS, enter the four-character confirmation code sent to %{sms_number}.
    TEXT
  %>
  <br>
  {{#t}}Code is case sensitive.{{/t}}
  </div>
  <div style="text-align: center;">
    <label for="code" class="screenreader-only">{{#t}}Confirmation code (required){{/t}}</label>
    <input type="text" style="width: 100px; font-size: 1.5em;" name="code" id="code" aria-describedby='sms_confirmation_instructions' required="true"/><br/>
    <span style="display: none;" class="channel_id">&nbsp;</span>
    <a href="<%= registration_confirmation_path("{{ code }}") %>" style="display: none;" class="register_channel_link">&nbsp;</a>
  </div>
  <div style="margin: 0 0 5px; text-align: center; font-size: 0.8em;">
    <a href="#" class="re_send_confirmation_link">{{#t}}Re-Send Confirmation{{/t}}</a>
  </div>
  <div class="status_message" role="status" aria-live="polite" style="visibility: hidden; text-align: center; color: #444; font-weight: bold">{{#t}}Confirming...{{/t}}</div>
  <div class="button-container">
    <button type="submit" class="btn">{{#t}}Confirm{{/t}}</button>
  </div>
<% end %>
<div style="display: none;" id="select_templates">
  {{> views/email_select}}
  {{> views/sms_select}}
  {{> views/twitter_select}}
  {{> views/yo_select}}
</div>
